/*
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Modified by Stefan Tillich, University of Bristol
 * - Adapted from TracksActivity.java
 * - Changed package name
 */

package org.iacr.android.apps.yaea.ui.phone;

import org.iacr.android.apps.yaea.ui.BaseSinglePaneActivity;
import org.iacr.android.apps.yaea.ui.RumpSessionFragment;

import org.iacr.android.apps.yaea.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.widget.EditText;



public class RumpSessionActivity extends BaseSinglePaneActivity {
	
	public static final int USER_PSEUDONYM_DIALOG = 0;
	public static final int VOTE_CANCEL_DIALOG = 1;
	public static final int UNVOTE_CANCEL_DIALOG = 2;
	public static final int VOTE_UNVOTE_CANCEL_DIALOG = 3;
	public static final int VOTES_AT_MAX_ALERT = 4;
	public static final int NO_PSEUDONYM_ALERT = 5;
	public static final int VOTING_NOT_YET_STARTED_ALERT = 6;
	public static final int VOTING_ENDED_ALERT = 7;
	public static final int VOTE_PSEUDONYM_UNKNOWN = 8;
	public static final int VOTE_RECORDED_OK = 9;
	public static final int VOTE_UNKNOWN_SERVER_RESPONSE = 10;
	public static final int VOTE_COMMUNICATION_ERROR = 11;
	
	private RumpSessionFragment rumpSessionFragment;
	
    @Override
    protected Fragment onCreatePane() {
    	rumpSessionFragment = new RumpSessionFragment();
    	return rumpSessionFragment;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
    	// HACK!!!
    	// Seems to work on first glance
    	if (rumpSessionFragment == null) {
    		rumpSessionFragment = (RumpSessionFragment)getSupportFragmentManager().findFragmentById(R.id.root_container);
    	}
    }
    

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getActivityHelper().setupSubActivity();
    }
    
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
    	
    	Dialog dialog = null;
    	
    	switch (id) {
    	
    		case USER_PSEUDONYM_DIALOG: {
    	        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Input your user pseudonym");
    	        final EditText edit = new EditText(this);
    	        
    	        // Get current user pseudonym (if any) and set it as default
    	        // value for the text box
    	        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        		String user_pseudonym = sp.getString("user_pseudonym", null);
        		if (user_pseudonym != null) {
        			edit.setText(user_pseudonym);
        		}
    	        
    	        builder.setView(edit);
    	        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    	        	public void onClick(DialogInterface dialog, int which) {	        		
    	        		String user_pseudonym = edit.getText().toString();
    	        		RumpSessionActivity.this.setUserPseudonym(RumpSessionActivity.this, user_pseudonym);
    	        		
    	        		dialog.dismiss();
    	            }
    	        });
    	        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    	        	public void onClick(DialogInterface dialog, int which) {
    	        		dialog.dismiss();
    	            }
    	        });
    	        dialog = builder.create();
    	        break;
    		}
    		
    		case VOTE_CANCEL_DIALOG: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Add a vote for this talk?");
    	        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
    	        	public void onClick(DialogInterface dialog, int which) {
    	        		rumpSessionFragment.handleCurrentVote(true);
    	        		dialog.dismiss();
    	            }
    	        });
    	        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;
    		}
    		
    		case UNVOTE_CANCEL_DIALOG: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Remove a vote for this talk?");
    	        builder.setPositiveButton("Remove", new DialogInterface.OnClickListener() {
    	        	public void onClick(DialogInterface dialog, int which) {
    	        		rumpSessionFragment.handleCurrentVote(false);
    	        		dialog.dismiss();
    	            }
    	        });
    	        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;
    		}
    		
    		case VOTE_UNVOTE_CANCEL_DIALOG: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Add or remove a vote for this talk?");
    	        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
    	        	public void onClick(DialogInterface dialog, int which) {
    	        		rumpSessionFragment.handleCurrentVote(true);
    	        		dialog.dismiss();
    	            }
    	        });
    	        builder.setNegativeButton("Remove", new DialogInterface.OnClickListener() {
    	        	public void onClick(DialogInterface dialog, int which) {
    	        		rumpSessionFragment.handleCurrentVote(false);
    	        		dialog.dismiss();
    	            }
    	        });
    	        builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;
    		}
    		
    		case VOTES_AT_MAX_ALERT: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("You already cast the maximum number of votes!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;    			
    		}
    		
    		case NO_PSEUDONYM_ALERT: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("To vote, set your user pseudonym!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;    			
    		}
    		
    		case VOTING_NOT_YET_STARTED_ALERT: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Voting has not started yet!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;   			
    		}
    		
    		case VOTING_ENDED_ALERT: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Voting has ended!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;   			
    		}
    		
    		case VOTE_PSEUDONYM_UNKNOWN: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Voting server: User pseudonym unknown!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;   			
    		}
    		
    		case VOTE_RECORDED_OK: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Voting server: Your vote was recorded.");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;   			
    		}
    		
    		case VOTE_UNKNOWN_SERVER_RESPONSE: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("Voting server returned unknown response!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;   			
    		}
    		
    		case VOTE_COMMUNICATION_ERROR: {
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	        builder.setIcon(android.R.drawable.ic_dialog_info);
    	        builder.setTitle("A communication error occured during voting!");
    	        builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();						
					}
				});
    	        dialog = builder.create();
    	        break;   			
    		}    		
    		
    	}

        return dialog;
    }
    
    private void setUserPseudonym(final Context context, final String user_pseudonym) {
    	
    	// Set the new user pseudonym in the RumpSessionFragment
    	rumpSessionFragment.setUserPseudonym(user_pseudonym);
    	
    	// Write new user pseudonym to shared prefs in the background
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... voids) {                
            	SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
                //sp.edit().putBoolean("accepted_eula", true).commit();
                sp.edit().putString("user_pseudonym", user_pseudonym).commit();
                return null;
            }
        }.execute();
    }
}
